package Demo69;

/**
 * 删除字符中的所有相邻重复项[栈]
 * https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/
 * 使用stringBuffer来模拟栈, 来实现去重
 */
class Solution {
    public String removeDuplicates(String str) {
        StringBuffer ret = new StringBuffer();
        int i = 0;
        while(i < str.length()) {
            if(ret.length() > 0 && ret.charAt(ret.length() - 1) == str.charAt(i)) {
                // 出栈
                ret.deleteCharAt(ret.length() - 1);
            }else {
                // 进栈
                ret.append(str.charAt(i));
            }
            i++;
        }
        return ret.toString();
    }
}